//
//  DFFunctionGenerator.m
//  dfCommandBuilder
//
//  Created by Tom Booth on 15/06/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "DFFunctionGenerator.h"

@interface DFFunctionGenerator (PrivateMethods)

- (NSString*) simpleFunctionForCommand:(DFCommand*) command;
- (NSString*) rowFunctionForCommand:(DFCommand*) command;

- (NSString*) parametersToString:(NSArray*) parameters;

@end


@implementation DFFunctionGenerator

NSMutableString *header;
NSMutableString *types;


- (void) writeToDirectory:(NSString*) directory :(NSArray*) functions {
	
	// go through all the functions
	for (DFCommand *c in functions) {
		if (c.createsRows) {
			fprintf(stdout, "%s", [[self rowFunctionForCommand:c] cStringUsingEncoding:NSASCIIStringEncoding]);
		}
		else if (c.returnsRows) {
			// Should only be getrows which we don't really want to include
		}
		else {
			fprintf(stdout, "%s", [[self simpleFunctionForCommand:c] cStringUsingEncoding:NSASCIIStringEncoding]);
		}
	}
	
}




- (NSString*) simpleFunctionForCommand:(DFCommand*) command {
	NSMutableString *function = [[NSMutableString alloc] init];
	
	[function appendFormat:@"int df_%@(%@void (*callback)(void));\n", [[command name] lowercaseString], [self parametersToString:[command getParameters]]];
	
	return function;
}



- (NSString*) rowFunctionForCommand:(DFCommand*) command {
	return @"\n";
}



- (NSString*) parametersToString:(NSArray*) parameters {
	NSMutableString *output = [[NSMutableString alloc] init];
	
	for (DFParameter *p in parameters) {
		switch (p.type) {
			case DFStringParameter:
				[output appendFormat:@"char *%@, ", p.name];
				break;
			case DFNumberParameter:
				[output appendFormat:@"int %@, ", p.name];
				break;
			case DFBooleanParameter:
				[output appendFormat:@"int %@, ", p.name];
				break;
			case DFTimeParameter:
				[output appendFormat:@"df_time *%@, ", p.name];
				break;
			case DFDateParameter:
				[output appendFormat:@"df_date *%@, ", p.name];
				break;
			case DFAddressParameter:
				[output appendFormat:@"char *%@, ", p.name];
				break;
			case DFLongParameter:
				[output appendFormat:@"long %@, ", p.name];
				break;
			default:
				break;
		}
	}
	
	return output;
}

@end
